package com.tile.v2;

import java.util.Date;
import java.util.concurrent.ExecutorService;

public class LevelTile extends Thread{

	private Integer minx;
	private Integer maxx;
	public Integer miny;
	public Integer maxy;
	private Integer zoom;
	private ExecutorService pool;
	private String despath;
	private String servicepath;
	private int tn;
	private int totaltn;

	public int getTotaltn() {
		return totaltn;
	}
	public void setTotaltn(int totaltn) {
		this.totaltn = totaltn;
	}
	public int getTn() {
		return tn;
	}
	public void setTn(int tn) {
		this.tn = tn;
	}
	public ExecutorService getPool() {
		return pool;
	}
	public void setPool(ExecutorService pool) {
		this.pool = pool;
	}
	public String getDespath() {
		return despath;
	}
	public void setDespath(String despath) {
		this.despath = despath;
	}
	public String getServicepath() {
		return servicepath;
	}
	public void setServicepath(String servicepath) {
		this.servicepath = servicepath;
	}
	public LevelTile(){
		
	}
	public Integer getMinx() {
		return minx;
	}
	public void setMinx(Integer minx) {
		this.minx = minx;
	}
	public Integer getMaxx() {
		return maxx;
	}
	public void setMaxx(Integer maxx) {
		this.maxx = maxx;
	}
	public Integer getMiny() {
		return miny;
	}
	public void setMiny(Integer miny) {
		this.miny = miny;
	}
	public Integer getMaxy() {
		return maxy;
	}
	public void setMaxy(Integer maxy) {
		this.maxy = maxy;
	}
	public Integer getZoom() {
		return zoom;
	}
	public void setZoom(Integer zoom) {
		this.zoom = zoom;
	}
	
	public void downloadTiles(){
		
	}
	
	/**
	 * 丢失的文件
	 * @param source
	 * @param des
	 */
	public synchronized void downloadLostTiles(Tile tile, String des){
		System.out.println("*********************下载丢失切片"+des);
		DownloadThread t = new DownloadThread(tile, despath, servicepath, this);
		t.run();
	}
	
	@Override
	public void run() {
		System.out.println("线程"+this.tn+"开始");
		Date date = new Date();
		int pre =(maxy - miny) / this.totaltn;
		if(pre == 0){
			pre = 1;
		}
		int starty = miny + pre * this.tn;
		int endy = miny + pre * (this.tn+1);
		endy = endy <= maxy ? endy : maxy;
		for(int j= starty; j< endy; j++){
			for(int i=minx; i<= maxx; i++){
				Tile tile = new Tile(i, j, zoom);
				DownloadThread t = new DownloadThread(tile, despath, servicepath, this);
				t.run();
			}
		}
		Date date1 = new Date();
		
		double time = (date1.getTime()-date.getTime())/1000/60.0;
		System.out.println("线程"+this.tn+"结束;总共花了"+time+"分");
	}
}
